package com.yanchangapp.ui.activity._n_manage;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yanchangapp.R;
import com.yanchangapp.adapter._n_manage.AttachmentAdapter;
import com.yanchangapp.adapter._n_manage.OperateBookAdapter;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.commom.LoadingType;
import com.yanchangapp.commom.OrderStatus;
import com.yanchangapp.commom._PlanStatus;
import com.yanchangapp.model.bean.AttachBean;
import com.yanchangapp.model.bean.OperateBookBean;
import com.yanchangapp.model.response.PlanDetailResponse;
import com.yanchangapp.ui.activity.mail.AttachmentShowActivity;
import com.yanchangapp.ui.presenter.PlanDetailAtPresenter;
import com.yanchangapp.ui.view.PlanDetailAtView;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.util.ui.ResHelper;
import com.zxl.baselib.util.ui.UIUtils;
import com.zxl.baselib.widget.NiceNestedScrollView;
import com.zxl.baselib.widget.YCLoadingView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * page: 计划详情页面...
 * <p>
 * 用户查看具体的今日计划 并可对其进行显示等操作...
 * <p>
 * 暂时缺少 心得详情...包括历史计划详情也需含有此信息
 */
public class PlanDetailActivity extends BaseActivity<PlanDetailAtView, PlanDetailAtPresenter> implements PlanDetailAtView {
    @BindView(R.id.ivToolbarNavigation)
    ImageView ivToolbarNavigation;
    @BindView(R.id.vToolbarDivision)
    View vToolbarDivision;
    @BindView(R.id.tvToolbarTitle)
    TextView tvToolbarTitle;
    @BindView(R.id.btnMakePlan)
    Button btnMakePlan;
    @BindView(R.id.bottom)
    LinearLayout bottom;
    @BindView(R.id.tvWorkItemTitle)
    TextView tvWorkItemTitle;
    @BindView(R.id.tvWorkItemName)
    TextView tvWorkItemName;
    @BindView(R.id.tvStrandTitle)
    TextView tvStrandTitle;
    @BindView(R.id.tvStrandTime)
    TextView tvStrandTime;
    @BindView(R.id.tvRealStartTitle)
    TextView tvRealStartTitle;
    @BindView(R.id.tvRealStartTime)
    TextView tvRealStartTime;
    @BindView(R.id.rvOperateBook)
    RecyclerView rvOperateBook;
    @BindView(R.id.llAttach)
    LinearLayout llAttach;
    @BindView(R.id.btnNextRefresh)
    TextView btnNextRefresh;
    @BindView(R.id.llErrorNetwork)
    LinearLayout llErrorNetwork;
    @BindView(R.id.ycLoading)
    YCLoadingView ycLoading;
    @BindView(R.id.nestScrollview)
    NiceNestedScrollView nestScrollview;
    @BindView(R.id.rlStartTime)
    RelativeLayout rlStartTime;
    @BindView(R.id.rlEndTime)
    RelativeLayout rlEndTime;
    @BindView(R.id.rvAttachmentLists)
    RecyclerView rvAttachmentLists;
    private static final String PLAN_ID = "PLAN_ID";
    @BindView(R.id.tvEndWorkTitle)
    TextView tvEndWorkTitle;
    @BindView(R.id.tvEndWork)
    TextView tvEndWork;
    @BindView(R.id.tvSummaryTitle)
    TextView tvSummaryTitle;
    @BindView(R.id.tvSummaryContent)
    TextView tvSummaryContent;
    @BindView(R.id.tvTag)
    TextView mTvTag;
    @BindView(R.id.llReason)
    LinearLayout llReason;
    private long planID;
    private boolean isDailyDetail = false;
    private OperateBookAdapter mOperateBookAdapter;
    private List<OperateBookBean> mOperateBookBeans = new ArrayList<>();
    private AttachmentAdapter mAttachmentAdapter;
    private List<AttachBean> mAttachBeans = new ArrayList<>();
    private void setLoadingState(LoadingType loadingType) {
        switch (loadingType) {
            case LOADING:
                if (ycLoading.getVisibility() == View.GONE) {
                    ycLoading.setVisibility(View.VISIBLE);
                }

                if (llErrorNetwork.getVisibility() == View.VISIBLE) {
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (nestScrollview.getVisibility() == View.VISIBLE) {
                    nestScrollview.setVisibility(View.GONE);
                    bottom.setVisibility(View.GONE);
                }
                break;
            case SUCCESS:
                if (ycLoading.getVisibility() == View.VISIBLE) {
                    ycLoading.setVisibility(View.GONE);
                }


                if (llErrorNetwork.getVisibility() == View.VISIBLE) {
                    llErrorNetwork.setVisibility(View.GONE);
                }

                // 设置布局隐藏
                if (nestScrollview.getVisibility() == View.GONE) {
                    nestScrollview.setVisibility(View.VISIBLE);
                    bottom.setVisibility(View.VISIBLE);
                }

                break;
            case ERROR:
                if (ycLoading.getVisibility() == View.VISIBLE) {
                    ycLoading.setVisibility(View.GONE);
                }

                if (llErrorNetwork.getVisibility() == View.GONE) {
                    llErrorNetwork.setVisibility(View.VISIBLE);
                }

                // 设置布局隐藏
                if (nestScrollview.getVisibility() == View.VISIBLE) {
                    nestScrollview.setVisibility(View.GONE);
                    bottom.setVisibility(View.GONE);
                }
                break;
        }
    }

    /**
     * @param activity 活动
     * @param planID   计划ID
     */
    public static void show(Activity activity, long planID) {
        Intent intent = new Intent(activity, PlanDetailActivity.class);
        intent.putExtra(PLAN_ID, planID);
        activity.startActivity(intent);
    }

    public static void show(Activity activity, long planID,boolean isDaily) {
        Intent intent = new Intent(activity, PlanDetailActivity.class);
        intent.putExtra(PLAN_ID, planID);
        intent.putExtra("isDaily",isDaily);
        activity.startActivity(intent);
    }

    @Override
    protected void init() {
        // 获取传递的数值...
        planID = getIntent().getLongExtra(PLAN_ID, -1);
        isDailyDetail = getIntent().getBooleanExtra("isDaily",false);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_plan_detail;
    }

    @Override
    protected PlanDetailAtPresenter createPresenter() {
        return new PlanDetailAtPresenter(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        if (isDailyDetail){
            tvToolbarTitle.setText("今日计划详情");
        }else {
            tvToolbarTitle.setText("历史计划详情");
        }
        initAdapter();
    }

    private void initAdapter() {
        LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        manager.setSmoothScrollbarEnabled(true);
        manager.setAutoMeasureEnabled(true);
        rvOperateBook.setLayoutManager(manager);
        rvOperateBook.setHasFixedSize(true);
        rvOperateBook.setNestedScrollingEnabled(false);
        mOperateBookAdapter = new OperateBookAdapter(mOperateBookBeans);
        rvOperateBook.setAdapter(mOperateBookAdapter);

        // 设置附件的adapter类
        LinearLayoutManager attachmentManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        attachmentManager.setSmoothScrollbarEnabled(true);
        attachmentManager.setAutoMeasureEnabled(true);
        rvAttachmentLists.setLayoutManager(attachmentManager);
        mAttachmentAdapter = new AttachmentAdapter(mAttachBeans);
        rvAttachmentLists.setAdapter(mAttachmentAdapter);
    }

    @Override
    protected void initData() {
        setLoadingState(LoadingType.LOADING);
        mPresenter.queryPlanDetail(planID);
    }

    @Override
    protected void initListener() {
        ivToolbarNavigation.setOnClickListener(v -> finish());

        mAttachmentAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.tvLook){
                AttachmentShowActivity.show(PlanDetailActivity.this,mAttachBeans.get(position).getAttachmentUrl());
            }
        });

        btnNextRefresh.setOnClickListener(v -> initData());

        btnMakePlan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddFinishActivity.show(PlanDetailActivity.this,planID);
            }
        });
    }

    @Override
    public void queryPlanDetailSuccess(PlanDetailResponse planDetailResponse) {
        // 如果心得为空,显示补写心得
        // 依据不同的状态设置不同的颜色,设置时间显示状态
        setLoadingState(LoadingType.SUCCESS);
        mTvTag.setText(planDetailResponse.getScheduleStatusName());
        if (planDetailResponse.getScheduleStatus() == _PlanStatus.NOT_START.getStatus()){
            mTvTag.setBackground(ResHelper.getDrawable(R.drawable.tag_order_default));
            rlStartTime.setVisibility(View.GONE);
            rlEndTime.setVisibility(View.GONE);
            bottom.setVisibility(View.GONE);
            llReason.setVisibility(View.GONE);
            tvSummaryContent.setVisibility(View.GONE);
        }else if (planDetailResponse.getScheduleStatus() == _PlanStatus.LOADING.getStatus()){
            mTvTag.setBackground(ResHelper.getDrawable(R.drawable.tag_order_submit));
            rlStartTime.setVisibility(View.VISIBLE);
            rlEndTime.setVisibility(View.GONE);
            tvRealStartTime.setText(planDetailResponse.getStartTime());
            bottom.setVisibility(View.GONE);
            llReason.setVisibility(View.GONE);
            tvSummaryContent.setVisibility(View.GONE);
        }else if (planDetailResponse.getScheduleStatus() == _PlanStatus.CAN_FINISH.getStatus()){
            mTvTag.setBackground(ResHelper.getDrawable(R.drawable.tag_order_pass));
            tvRealStartTime.setText(planDetailResponse.getStartTime());
            tvEndWork.setText(planDetailResponse.getEndTime());
            tvSummaryTitle.setText("完成心得：");
            if (!TextUtils.isEmpty(planDetailResponse.getConclusion())){
                tvSummaryContent.setText(planDetailResponse.getConclusion());
                bottom.setVisibility(View.GONE);
            }else {
                bottom.setVisibility(View.VISIBLE);
            }
        }else {
            mTvTag.setBackground(ResHelper.getDrawable(R.drawable.tag_order_reject));
            tvRealStartTime.setText(planDetailResponse.getStartTime());
            tvEndWork.setText(planDetailResponse.getEndTime());
            bottom.setVisibility(View.GONE);
            tvSummaryTitle.setText("不能完成原因：");
            tvSummaryContent.setText(planDetailResponse.getConclusion());
        }
        // 设置标题
        tvWorkItemName.setText(planDetailResponse.getTitle());
        if (planDetailResponse.getStandardTime() == null){
            tvStrandTime.setText("暂无");
        }else {
            tvStrandTime.setText(getString(R.string.strand_time,planDetailResponse.getStandardTime(),planDetailResponse.getStandardTimeTypeName()));
        }

        mOperateBookBeans.clear();
        mOperateBookBeans.addAll(planDetailResponse.getOperationList());
        mOperateBookAdapter.notifyDataSetChanged();

        if ((planDetailResponse.getAttachments() != null)
                && (planDetailResponse.getAttachments().size() != 0)){
            mAttachBeans.addAll(planDetailResponse.getAttachments());
            mAttachmentAdapter.notifyDataSetChanged();
        }else {
            llAttach.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == AppConstant.RECODE_FROM_HISTOTY_DETAIL) {
                String info = data.getStringExtra("data");
                if (!TextUtils.isEmpty(info)) {
                    tvSummaryContent.setText(info);
                    btnMakePlan.setVisibility(View.GONE);
                }
            }else if (requestCode == AppConstant.RECODE_FROM_ADD_FINISH){
                String info = data.getStringExtra("data");
                tvSummaryContent.setText(info);
                btnMakePlan.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void queryPlanDetailFailure(String err) {
        UIUtils.showToast(err);
        setLoadingState(LoadingType.ERROR);
    }

}
